package com.iring.dao.vos;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.iring.util.common.StringUtil;
import com.iring.util.vos.VOSDataUtil;

/**
 * 基础操作类
 *
 * @author wanggan
 */
@Configuration
public class BaseVosDao<T> {

    @Autowired
    VOSDataUtil util;

    private static final Logger logger = LoggerFactory.getLogger(BaseVosDao.class);

    private static ObjectMapper mapper = new ObjectMapper();

    /**
     * 将vos返回的json字符串转换成相应的类对象
     *
     * @param result
     *            json结果
     * @param clazz
     *            需要转换成的类
     * @return 对应的类对象
     */
    public T convertToObject(String result, Class<T> clazz) {
        if(StringUtil.isEmpty(result)){
            return null;
        }

        // 设置忽略属性
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        T t = null;
        try {
            t = mapper.readValue(result, clazz);
        } catch (IOException e) {
           logger.info("json={} convert to object failed",result);
        }
        return t;
    }
}
